#include "FKNNGameScence.h"
#include "Game/GameLib.h"

#include "Main/stdafx.h"
#include "CMD_FKOx.h"
#include "FKNNPlayer.h"
#include "Games/NiuNiuGameLogic.h"
#include "GameLib/JniCross/JniFun.h"
#include "Game/Game/MissionWeiXin.h"

#include "Games/GameChatLayer.h"
#include "FKNNRecordLayer.h"
#include "FKNNSoundFun.h"

void FKNNGameScence::initButton()
{
	auto btn = (Button*)_layout->getChildByName("Button_ready");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));

	btn = (Button*)_layout->getChildByName("Button_addFriend");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_back");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_dismiss");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_exit");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_chat");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_record");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_talk");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));

	for (int i = 0; i < BTN_MAX; ++i)
	{
		btn = (Button*)_layout->getChildByName(StringUtils::format("Button_opt%d", i));
		btn->addTouchEventListener(CC_CALLBACK_2(FKNNGameScence::BtnCallBack, this));
		m_btns[i] = btn;
	}
	m_btnAdd = (Button*)_layout->getChildByName("Button_optAdd");
}
void FKNNGameScence::BtnOptCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
		case cocos2d::ui::Widget::TouchEventType::ENDED:
		{
			FKNNSoundFun::playEffect("effect_click");

			TextAtlas* testAtlas = (TextAtlas*)btn->getChildByName("AtlasLabel_value");
			std::string btnIndex = testAtlas->getString();

			CMD_C_FLY_AddScore flyAddScore;
			flyAddScore.desChairID = atoi(btnIndex.substr(0, 1).c_str());
			flyAddScore.desCardNum = atoi(btnIndex.substr(btnIndex.size() - 1, btnIndex.size()).c_str());
			flyAddScore.lScore = 1;
			SendSocketData(SUB_C_FLY_ADD_SCORE, &flyAddScore, sizeof(flyAddScore));

			hiddenAllOptBtns();
			log("BtnOptCallBack---------in--------chairId:%d,desCardNum:%d,lscore:%d", flyAddScore.desChairID, flyAddScore.desCardNum, flyAddScore.lScore);
		}
	}
}
void FKNNGameScence::BtnCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		FKNNSoundFun::playEffect("effect_click");
		if (btn->getName().compare("Button_ready") == 0)
		{
			sendReady();
			defaultState();
		}
		else if (btn->getName().compare("Button_addFriend") == 0)
		{
			auto txt = (Text*)_layout->getChildByName("Sprite_table")->getChildByName("Text_deskNo");
			//MissionWeiXin::Instance().shareTextWeiXin(utility::getScriptReplaceValue("PrivateTxt1", txt->getString()));
			MissionWeiXin::Instance().shareUrlWeiXin("http://www.rdaohy.com/", utility::getScriptString("PrivateTxt4"), utility::getScriptReplaceValue("PrivateTxt1", txt->getString()));
		}
		else if (btn->getName().compare("Button_back") == 0 || btn->getName().compare("Button_exit") == 0)
		{
			Button_ExitGameBase();
		}
		else if (btn->getName().compare("Button_dismiss") == 0)
		{
			CMD_GR_Dismiss_Private kNetInfo;
			kNetInfo.bDismiss = 1;
			CServerItem::get()->SendSocketData(MDM_GR_PRIVATE, SUB_GR_PRIVATE_DISMISS, &kNetInfo, sizeof(kNetInfo));
			/*
			auto dismissDialog = CSLoader::createNode("aqmj/commonLayer.csb");
			addChild(dismissDialog, 201, 201);
			dismissDialog->setName("disMissDialog");

			Button* btn = (Button*)dismissDialog->getChildByName("Button_cancel");
			btn->addClickEventListener([=](Ref* pSender){
				dismissDialog->removeFromParentAndCleanup(true);
			});

			btn = (Button*)dismissDialog->getChildByName("Button_confirm");
			btn->addClickEventListener([=](Ref* pSender){
				dismissDialog->removeFromParentAndCleanup(true);

				CMD_GR_Dismiss_Private kNetInfo;
				kNetInfo.bDismiss = 1;
				CServerItem::get()->SendSocketData(MDM_GR_PRIVATE, SUB_GR_PRIVATE_DISMISS, &kNetInfo, sizeof(kNetInfo));
			});
			*/
		}
		else if (btn->getName().compare("Button_chat") == 0)
		{
			GameChatLayer* gChatLayer = GameChatLayer::create();
			addChild(gChatLayer);
			gChatLayer->showRightPos();
		}
		else if (btn->getName().compare("Button_record") == 0)
		{
			FKNNRecordLayer* rLayer = FKNNRecordLayer::create();
			addChild(rLayer);
		}
		else if (btn->getName().compare("Button_talk") == 0)
		{
			Sprite* sprTalk = (Sprite*)_layout->getChildByName("sprite_talk");
			if (sprTalk->isVisible())
			{
				JniFun::stopSoundRecord();
				sprTalk->setVisible(false);
				return;
			}
			sprTalk->setVisible(true);
			JniFun::startSoundRecord();
			int iTimeID = TimeManager::Instance().addCerterTimeCB(TIME_CALLBACK(FKNNGameScence::endButtonTalk, this), 5.0f)->iIdex;
			sprTalk->setUserData((void*)iTimeID);
		}
		else if (btn->getName().compare("Button_opt0") == 0)
		{
			CMD_C_CALL_CARD callCard;
			callCard.lCount = 1;
			SendSocketData(SUB_C_CALL_CARD, &callCard, sizeof(callCard));
 
 			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_opt1") == 0)
		{
			CMD_C_CALL_CARD callCard;
			callCard.lCount = 2;
			SendSocketData(SUB_C_CALL_CARD, &callCard, sizeof(callCard));

			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_opt2") == 0)
		{
			CMD_C_CALL_CARD callCard;
			callCard.lCount = 3;
			SendSocketData(SUB_C_CALL_CARD, &callCard, sizeof(callCard));

			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_continue") == 0)
		{
			_layout->getChildByName("Panel_result")->setVisible(false);
			sendReady();
			defaultState();
		}
		else if (btn->getName().compare("Button_share") == 0)
		{
			static FKNNGameScence* pTemp = NULL;
			pTemp = this;
			cocos2d::utils::captureScreen([](bool b, std::string name){
				if (b)
				{
					MissionWeiXin::Instance().shareScreenWeiXin(name);
					pTemp->Button_ExitGameBase();
					pTemp->ExitGame();
					pTemp->_layout->getChildByName("finish_node")->setVisible(false);
				}
			}, "screenshot.png");
		}
	}
		break;
	default:
		break;
	}
}

void FKNNGameScence::endButtonTalk()
{
	Sprite* sprTalk = (Sprite*)_layout->getChildByName("sprite_talk");
	if (!sprTalk->isVisible())
	{
		return;
	}
	int iTimeID = (uintptr_t)sprTalk->getUserData();
	TimeManager::Instance().removeByID(iTimeID);
	sprTalk->setVisible(false);
	std::string kFileName = JniFun::stopSoundRecord();
	sendTalkFile(m_pLocal->GetChairID(), kFileName);
}

void FKNNGameScence::BtnHeadCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		int userIndex = (intptr_t) btn->getUserData();
		FKNNPlayer* pPlayer = m_pPlayer[userIndex];
		MessageBox("show userInfo<===========", "title");
// 		PopScence::Instance().showIPInfo(pPlayer->GetNickName(),
// 			utility::paseInt(pPlayer->GetUserID(), 6),
// 			pPlayer->GetUserInfo()->szLogonIP,
// 			pPlayer->GetUserInfo()->szHeadHttp);
	}
		break;
	default:
		break;
	}
}
